<template>
  <div style="padding: 10px;width: 100%" >


    <!--    搜索区-->
    <!--    <div style="margin: 10px 0">
          <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search"></el-input>
          <el-input style="width: 200px" placeholder="请输入邮箱" suffix-icon="el-icon-message" class="ml-5"></el-input>
          <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position" class="ml-5"></el-input>
          <el-button class="ml-5" type="primary">搜索</el-button>
        </div>-->

    <div style="margin: 10px">
      <el-input v-model="search" placeholder="请输入义工信息" style="width:200px" clearable/>
      <el-button type="primary" style="margin-left: 5px" @click="load" color="#3c9d19">查询</el-button>
    </div>


    <!--    功能区-->


    <div style="margin: 10px">
      <el-button type="primary" @click="add" color="#3c9d19">新增</el-button>

      <!-- <el-button type="primary" @click="exp">导出 <i class="el-icon-top"></i></el-button> -->

    </div>

    <!--    搜索区-->



    <el-table :data="tableData" border stripe  style="width: 100%" >
      <el-table-column prop="id" label="ID" width="80"/>
      <el-table-column prop="profilePhoto" label="头像">
        <template #default="scope">
          <div class="block">
            <el-avatar shape="square" :size="60" :src="scope.row.profilePhoto" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="义工" width="100">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>性别: {{ scope.row.gender }}</div>
              <div>电话号码: {{ scope.row.phone }}</div>
              <div>身份证号码: {{ scope.row.idCard }}</div>
              <div>出生日期: {{ scope.row.birthday }}</div>
            </template>
            <template #reference>
              <el-tag type="info">{{ scope.row.name }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="flag" label="标识" />

      <el-table-column prop="checkinDate" label="访问日期" />
      <el-table-column prop="checkoutDate" label="离职日期" />

      <el-table-column label="创建者" width="100">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>创建时间: {{ scope.row.created }}</div>
              <div>创建者: {{ scope.row.createdBy }}</div>
            </template>
            <template #reference>
              <el-tag type="info">{{ scope.row.createdBy }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="更新者" width="80">
        <template #default="scope">
          <el-popover effect="light" trigger="hover" placement="top" width="auto">
            <template #default>
              <div>更新时间: {{ scope.row.updated }}</div>
              <div>更新者: {{ scope.row.updatedBy }}</div>
            </template>
            <template #reference>
              <el-tag type="info">{{ scope.row.updatedBy }}</el-tag>
            </template>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" />

      <el-table-column fixed="right" label="Operations" >
        <template #default="scope">
          <el-button type="success" @click="handleEdit(scope.row)" round>编辑</el-button>
          <el-popconfirm title="确认删除？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button type="danger" round>删除 </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px">
      <el-pagination
          v-model:current-page="currentPage"
          :page-sizes="[5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog
        v-model="dialogVisible"
        title="义工信息"
        width="30%"
    >
      <el-form :model="form" label-width="120px">
        <el-form-item label="头像">
            <el-upload
                class="avatar-uploader"
                action="http://localhost:8081/file/upload/volunteer"
                :on-success="handleAvatarSuccess">
              <img v-if="form.profilePhoto" :src="form.profilePhoto" class="avatar">
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
        </el-form-item>

        <el-form-item label="工作人员姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="性别">
          <el-radio v-model="form.gender" label="男">男</el-radio>
          <el-radio v-model="form.gender" label="女">女</el-radio>
          <el-radio v-model="form.gender" label="未知">未知</el-radio>
        </el-form-item>
        <el-form-item label="标识">
          <el-input v-model="form.flag"></el-input>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码">
          <el-input v-model="form.idCard"></el-input>
        </el-form-item>
        <el-form-item label="出生日期">
          <el-date-picker
              v-model="form.birthday"
              type="datetime"
              placeholder="选择出生日期"
          />
      </el-form-item>
        <el-form-item label="入职日期">
          <el-date-picker
              v-model="form.checkinDate"
              type="datetime"
              placeholder="选择入职日期"
          />
        </el-form-item>
        <el-form-item label="离职日期">
          <el-date-picker
              v-model="form.checkoutDate"
              type="datetime"
              placeholder="选择离职日期"
          />
        </el-form-item>

        <el-form-item label="描述">
          <el-input v-model="form.description"></el-input>
        </el-form-item>

      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">
          确认
        </el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>




import request from "@/utils/request";

export default {
  name: 'staff',
  components: {

  },
  data(){
    return {
      form:{},
      dialogVisible:false,
      total:10,
      currentPage:1,
      pageSize:10,
      search:'',
      tableData:[],
      systemUser:{},
    }
  },
  created() {
    this.load()
  },
  methods:{
    load(){
      request.get("/volunteerInfo", {
        params:{
          pageNum:this.currentPage,
          pageSize:this.pageSize,
          search:this.search
        }
      }).then(res=>{
        console.log(res)
        this.tableData=res.data.records
        this.total=res.data.total
      })
    },

    add(){
      this.dialogVisible=true
      this.form={}
    },
    save(){
      this.systemUser=JSON.parse(localStorage.getItem("user"))
      if(this.form.id)
      {
        this.form.updatedBy=this.systemUser.id
        request.put("/volunteerInfo",this.form).then(res=>{

          if(res.code==='0')
          {
            this.$message({
              type:"success",
              message:"修改成功"
            })
          }
          else {
            this.$message({
              type:"error",
              message: res.msg
            })
          }


          this.load()
        })
      }else
      {
        this.form.createdBy=this.systemUser.id
        request.post("/volunteerInfo", this.form).then(res => {
          console.log(res)
          this.$message({
            type:"success",
            message:"新增成功"
          })
          this.load();
        })
      }

      this.dialogVisible=false
    },
  exp(){
      window.open("http://localhost:8002/volunteerInfo/export")
  },
    handleEdit(row){
      this.form=JSON.parse(JSON.stringify(row))
      this.dialogVisible=true
      console.log(this.form.volunteerInfoId)
    },

    handleSizeChange(pageSize){
      this.pageSize=pageSize;
      this.load()
    },
    handleCurrentChange(pageNum){
      this.currentPage=pageNum;
      this.load()
    },
    handleDelete(id){
      request.delete("/volunteerInfo/"+id).then(res=>{
        if(res.code==='0')
        {
          this.$message({
            type:"success",
            message:"删除成功"
          })
        }
        else {
          this.$message({
            type:"error",
            message: res.msg
          })
        }
        this.load()
      })
    },
    handleAvatarSuccess(res) {
      if (res.code === '0') {
        console.log(res)
        this.form.profilePhoto = res.data
        this.$message.success("上传成功")
      } else {
        this.$message.error("上传失败")
      }
    }
  }
}
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>

